/******************************************************************************
 * Product: Adempiere ERP & CRM Smart Business Solution                       *
 * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved.                *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA        *
 * or via info@compiere.org or http://www.compiere.org/license.html           *
 *****************************************************************************/
package org.compiere.model;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.DB;
import org.compiere.util.Env;

/**
 * Payment Callouts. org.compiere.model.CalloutPayment.*
 * @author Jorg Janke
 * @version $Id: CalloutPayment.java,v 1.3 2006/07/30 00:51:03 jjanke Exp $
 * @author Teo Sarca, SC ARHIPAC SERVICE SRL
 *         <li>BF [ 1803316 ] CalloutPayment: use C_Order.Bill_BPartner_ID
 * @author j2garcia - GlobalQSS
 *         <li>BF [ 2021745 ] Cannot assign project to payment with charge
 * @author Carlos Ruiz - GlobalQSS
 *         <li>BF [ 1933948 ] CalloutPayment working just with Draft Status
 */
public class CalloutFI_C_PaymentLine extends CalloutEngine
{

	/**
	 * Payment_Invoice. when Invoice selected - set C_Currency_ID -
	 * C_BPartner_ID - DiscountAmt = C_Invoice_Discount (ID, DateTrx) - PayAmt =
	 * invoiceOpen (ID) - Discount - WriteOffAmt = 0
	 * @param ctx context
	 * @param WindowNo current Window No
	 * @param mTab Grid Tab
	 * @param mField Grid Field
	 * @param value New Value
	 * @return null or error message
	 */
	public String CalcPayAmt(Properties ctx, int WindowNo, GridTab mTab,
		GridField mField, Object value)
	{
		int C_Payment_ID = Env.getContextAsInt(ctx, WindowNo, "C_Payment_ID");
		
		BigDecimal PayAmt = new BigDecimal(0);
		int currLine = mTab.getRecord_ID();
		
		List<MFI_C_PaymentLine> list = MFI_C_PaymentLine.getMFI_C_PaymentLines(log,  ctx, C_Payment_ID);
		for(int i=0; i < list.size(); i++ )
//			if (list.get(i).get_ID() == currLine)
//				PayAmt = PayAmt.add((BigDecimal)value);
//			else 
				PayAmt = PayAmt.add(list.get(i).getAmount());
		
		if(currLine == -1)
			PayAmt = PayAmt.add((BigDecimal)value);
	
		if(PayAmt.compareTo(new BigDecimal(0)) == 1){
			
			String sql = "UPDATE adempiere.C_Payment p"
					+ " SET PayAmt= ? "
					+ " WHERE C_Payment_ID=?";
				int no = DB.executeUpdateEx(sql, new Object[]{PayAmt, C_Payment_ID}, null);
		}
		return "";
	} // invoice


	
} // CalloutPayment
